﻿import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Layouts
import FilUI

Button {
    id: control

    property bool disabled                      : false
    property string contentDescription          : ""
    property color borderNormalColor            : Qt.rgba(136/255, 136/255, 136/255, 1)
    property color bordercheckedColor           : FilTheme.primaryColor
    property color borderHoverColor             : Qt.rgba(135/255, 135/255, 135/255, 1)
    property color borderDisableColor           : Qt.rgba(199/255, 199/255, 199/255, 1)
    property color borderPressedColor           : Qt.rgba(191/255, 191/255, 191/255, 1)
    property color normalColor                  : Qt.rgba(247/255, 247/255, 247/255, 1)
    property color checkedColor                 : FilTheme.primaryColor
    property color hoverColor                   : Qt.rgba(236/255, 236/255, 236/255, 1)
    property color checkedHoverColor            : Qt.lighter(checkedColor, 1.15)
    property color checkedPreesedColor          : Qt.lighter(checkedColor, 1.3)
    property color checkedDisableColor          : Qt.rgba(199/255, 199/255, 199/255, 1)
    property color disableColor                 : Qt.rgba(253/255, 253/255, 253/255, 1)
    property real size                          : 18
    property color textColor                    : {
        if (!enabled) {
            return Qt.rgba(161/255, 161/255, 161/255, 1)
        }
        return Qt.rgba(0, 0, 0, 1)
    }
    property bool textRight                     : true
    property real textSpacing                   : 6
    property bool animationEnabled              : FilTheme.animationEnabled
    property var clickListener                  : function() {
        checked = !checked
    }
    property bool indeterminate                 : false

    enabled                                     : !disabled
    onClicked                                   : clickListener()
    onCheckableChanged                          : {
        if (checkable) {
            checkable = false
        }
    }
    background                                  : Item {
        FilFocusRectangle {
            radius                              : 4
            visible                             : control.activeFocus
        }
    }
    focusPolicy                                 : Qt.TabFocus
    font                                        : FilTextStyle.Body
    horizontalPadding                           : 0
    verticalPadding                             : 0
    padding                                     : 0
    Accessible.role                             : Accessible.Button
    Accessible.name                             : control.text
    Accessible.description                      : contentDescription
    Accessible.onPressAction                    : control.clicked()
    contentItem                                 : RowLayout {
        spacing                                 : control.textSpacing
        layoutDirection                         : control.textRight ? Qt.LeftToRight : Qt.RightToLeft
        Rectangle {
            width                               : control.size
            height                              : control.size
            radius                              : 4
            border.color                        : {
                if (!enabled) {
                    return borderDisableColor
                }
                if (checked) {
                    return bordercheckedColor
                }
                if (pressed) {
                    return borderPressedColor
                }
                if (hovered) {
                    return borderHoverColor
                }
                return borderNormalColor
            }
            border.width                        : 1
            color                               : {
                if (checked) {
                    if (!enabled) {
                        return checkedDisableColor
                    }
                    if (pressed) {
                        return checkedPreesedColor
                    }
                    if (hovered) {
                        return checkedHoverColor
                    }
                    return checkedColor
                }
                if (!enabled) {
                    return disableColor
                }
                if (hovered) {
                    return hoverColor
                }
                return normalColor
            }
            FilIcon {
                anchors.centerIn                : parent
                iconSource                      : FilIcons.CheckboxIndeterminate
                iconSize: 14
                visible                         : indeterminate
                iconColor                       : Qt.rgba(1, 1, 1, 1)
                Behavior on visible {
                    enabled                     : control.animationEnabled
                    NumberAnimation{
                        duration                : 83
                    }
                }
            }

            FilIcon {
                anchors.centerIn                : parent
                iconSource                      : FilIcons.AcceptMedium
                iconSize                        : 14
                visible                         : checked && !indeterminate
                iconColor                       : Qt.rgba(1, 1, 1, 1)
                Behavior on visible {
                    enabled                     : control.animationEnabled
                    NumberAnimation {
                        duration                : 83
                    }
                }
            }
        }
        FilText {
            id: btn_text
            text                                : control.text
            Layout.alignment                    : Qt.AlignVCenter
            visible                             : text !== ""
            font                                : control.font
            color                               : control.textColor
        }
    }
}
